package com.isoft.demo01.controller;

import com.isoft.demo01.bean.BaseController;
import com.isoft.demo01.bean.ResponseData;
import com.isoft.demo01.bean.ResultCode;
import com.isoft.demo01.entity.Order;
import com.isoft.demo01.entity.Shop;
import com.isoft.demo01.entity.User;
import com.isoft.demo01.service.ShopService;
import com.isoft.demo01.service.impl.ShopImplService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpSession;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("shop")
public class ShopController extends BaseController {
    @Autowired
    ShopService shopService;
    @Autowired
    ShopImplService shopImplService;
    @PostMapping("/add")
    public ResponseData add(Integer gameid, Integer userid) {
             ResponseData req = new ResponseData();
            boolean result = shopImplService.addcart(userid, gameid);
            if (result) {
                req.setMsg(ResultCode.SUCCESS.getMessage());
                req.setErrorCode(ResultCode.SUCCESS.getCode());
            } else {
                req.setMsg(ResultCode.COMMON_FAIL.getMessage());
                req.setErrorCode(ResultCode.COMMON_FAIL.getCode());
            }
            req.setData(result);
            return req;
    }
    @GetMapping
    public ResponseData list(@RequestParam Integer userid){
            ResponseData req=new ResponseData();
            List<Shop> cartList = shopImplService.getCartVOList(userid);
            if (cartList!=null){
            req.setMsg(ResultCode.SUCCESS.getMessage());
            req.setErrorCode(ResultCode.SUCCESS.getCode());
            }else {
                req.setMsg(ResultCode.COMMON_FAIL.getMessage());
                req.setErrorCode(ResultCode.COMMON_FAIL.getCode());
            }
            req.setData(cartList);
            return req;

    }


   @DeleteMapping("/delete/{id}")
    public ResponseData delete(@PathVariable("id") int id) {
        ResponseData req = new ResponseData();
       boolean deletecart = shopImplService.deleteCart(id);
        if (deletecart) {
                req.setErrorCode(ResultCode.SUCCESS.getCode());
                req.setMsg(ResultCode.SUCCESS.getMessage());
            }else {
                req.setMsg(ResultCode.COMMON_FAIL.getMessage());
                req.setErrorCode(ResultCode.COMMON_FAIL.getCode());
        }
            req.setData(deletecart);
            return req;

    }
    @DeleteMapping("/deleteBatch")
    public ResponseData deleteBatch(Integer[] id,Integer userid){
            ResponseData req=new ResponseData();
            boolean deletecartids=shopImplService.deleteCartBatch(id,userid);
            if(deletecartids){
            req.setErrorCode(ResultCode.SUCCESS.getCode());
            req.setMsg(ResultCode.SUCCESS.getMessage());
            }else {
                req.setErrorCode(ResultCode.COMMON_FAIL.getCode());
                req.setMsg(ResultCode.COMMON_FAIL.getMessage());
            }
            req.setData(deletecartids);
            return req;

    }

    @DeleteMapping("/clear")
    public ResponseData clear(Integer userid){
        ResponseData req=new ResponseData();
        boolean clean=shopImplService.clearCart(userid);
        if(clean){
            req.setMsg(ResultCode.SUCCESS.getMessage());
            req.setErrorCode(ResultCode.SUCCESS.getCode());

            }else {
            req.setErrorCode(ResultCode.COMMON_FAIL.getCode());
            req.setMsg("购物车空空如也");
        }
            req.setData(clean);
            return req;


    }
    @GetMapping("allprice")
    public ResponseData allprice(Integer userid){
        ResponseData req=new ResponseData();
       int shop= shopImplService.allprice(userid);
        req.setErrorCode(ResultCode.SUCCESS.getCode());
        req.setMsg(ResultCode.SUCCESS.getMessage());
        req.setData(shop);
        return req;

    }

}
